package com.boot2.core.conf.upload;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.boot2.core.utils.StringUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

/**
 * @author zhangweilin
 * @Description: xxxxx
 * @date 2020/5/27
 */
@Data
@Configuration
@Component
public class UploadConf {

    @Autowired
    OssProperties ossProperties;

    /**
     * 尝试下单例
     * 当不写havingValue时，表示只判断是否为空
     * @return
     */
    @Bean
    @ConditionalOnProperty(prefix = "upload.ali", name = "accessKeyId")
    public OSS oss() {
        Assert.isTrue(ossProperties != null && StringUtils.isNotEmpty(ossProperties.getAccessKeyId()), "oss configuration can not be null,if you want to upload file to aliyun oss,pls reg it first,and get the accessKeyId and the accessKeySecret");
        OSSClientBuilder ossClientBuilder = new OSSClientBuilder();
        return ossClientBuilder.build(ossProperties.getEndPoint(), ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
    }

}
